package com.ynet.middleground.approve.listener.contract;

import java.util.List;
import java.util.Optional;

import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import com.ynet.core.exception.BusinessException;

/**
 * @author liulx
 * @description 更新合约模板状态监听，将合约模板状态更改为启用中
 * @date 2020-05-25 11:48
 */
@DependsOn("springContextUtils")
@Service
public class UpdateContractTemplateActivingListener extends UpdateContractTemplateStatusListener {

    @Override
    public void notify(DelegateExecution delegateExecution) {
        String operationUserId = getOperationUserId(delegateExecution);
        String channel = getChannel(delegateExecution);
        // 从流程中获取需要更新的模板 id 集合
        Object list = getObjectVariableInstance(delegateExecution, TEMPLATE_IDS_KEY);
        Optional.ofNullable(list)
            .orElseThrow(() -> new BusinessException("变量: " + TEMPLATE_IDS_KEY + " 不存在", "ECAP0506"));
        // 从流程中获取模板状态标志
        Object varTemplateStatus = getObjectVariableInstance(delegateExecution, TEMPLATE_STATUS_KEY);
        Optional.ofNullable(varTemplateStatus)
            .orElseThrow(() -> new BusinessException("变量: " + TEMPLATE_STATUS_KEY + " 不存在", "ECAP0506"));
        // 将模板状态更改为已启用
        Object templateStatus = "1";
        if (!"3".equals(varTemplateStatus) && list instanceof List<?>) {
            List<Integer> templateIds = (List<Integer>)list;
            producer.sendMessages(templateIds, templateStatus, channel, Integer.valueOf(operationUserId));
        }
    }

}
